﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace canyinguanlixitong
{
    public partial class frm采购商品 : Form
    {
        public frm采购商品()
        {
            InitializeComponent();
        }
        string str = "Data Source=.;Initial Catalog=Supermarket management system;Integrated Security=True";
     public  int danjuhao;
        public void serachId()
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            string sql1 = @"select danjuhaoid from dbo.单据号";
            SqlCommand com1 = new SqlCommand(sql1,conn);
            SqlDataReader rea1 = com1.ExecuteReader();
            while (rea1.Read())
            {
                danjuhao = Convert.ToInt32(rea1["danjuhaoid"]);
            }
     //       MessageBox.Show(danjuhao+"");
            rea1.Close();
        }
        int Id = 0;
        //退出
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void chaxun()
        {
            SqlConnection conn = new SqlConnection(str);
            DataSet ds = new DataSet("Student");
            string sql = @"select spID,spName,slLei,spJia from dbo.commodity,[Commodity type]
where commodity.spLei=[Commodity type].SlId ";
            SqlDataAdapter asdf = new SqlDataAdapter(sql, conn);
            asdf.Fill(ds, "Student");
            dgvqw1.DataSource = ds.Tables["Student"];
        }
        public int bianhao;

        public void fuzhi()
        {
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string sql = @"select spID,spName,slLei,spJia from dbo.commodity,[Commodity type]
where commodity.spLei=[Commodity type].SlId and spID='"+bianhao+"'";
            SqlCommand com = new SqlCommand(sql, con);
            SqlDataReader rea = com.ExecuteReader();
            while (rea.Read())
            {
                this.label4.Text = rea["spID"].ToString();
                label7.Text = rea["spName"].ToString();
              //  label8.Text = rea["商品规格"].ToString();
                textBox1.Text = rea["spJia"].ToString();
            //    this.textBox2.Text = rea["1"].ToString();
                Id = Convert.ToInt32(rea["spID"]);
                //Id1 = Convert.ToInt32(rea["库存数量"]);
                //供应商 = Convert.ToInt32(rea["供货ID"]);
            }
            rea.Close();
        }

//确定
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(str);
            conn.Open();
            int i=Convert.ToInt32( this.textBox2.Text);
            string sql = "update dbo.sangping set sum='" + i + "' where commid='" +Id + "'";
            SqlCommand com = new SqlCommand(sql, conn);
            int rea = com.ExecuteNonQuery();
            double money = i * Convert.ToDouble(textBox1.Text);
            string sql1 = @"insert into dbo.sangping(commid,danwei,sum,单据号) values('" + this.label4.Text.Trim() + "','千克','" + i + "','" + danjuhao + "')";
            SqlCommand com1 = new SqlCommand(sql1, conn);
          int rea1 = com1.ExecuteNonQuery();  
      //      MessageBox.Show(rea1+"");
            if (rea1 > 0 && rea > 0)
            {
                this.Close();
            }
            else
            {
                //  MessageBox.Show("错误");
                this.Close();
            }
        }

       
        //窗体加载
        private void frm采购商品_Load(object sender, EventArgs e)
        {
           
            chaxun();
            fuzhi();
            serachId();

        }

        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double num = Convert.ToDouble(this.textBox1.Text);
            double num1 = Convert.ToDouble(this.textBox2.Text);
            this.textBox1.Text = (num1 * num).ToString();
          
        }

     
        }
}
